<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['shopset']=plugin('jy_weishop')->plugin_setting('shopset');
$title=$return['pluginset']['title']?:'抖音客';
set_html('title',$title.'-'.$item['title']);
if($_RQ['op']=='main'){
	$id=intval($_RQ['id']);
	if($_RQ['code']){
		$authtoken=$this->model('model')->getAccessToken($_RQ['code']);
		if($authtoken['access_token']){
			$this->model('model')->getuserinfo($_SESSION['openid'],$authtoken['access_token']);
		}
		$return['authtoken']=$authtoken;
	}
	$item=$this->pdo_get('game',array('id'=>$id));
	$post=array(
		'agent_id'=>$_RQ['agent_id']?:$item['mid'],
		'mobile'=>$_RQ['mobile'],
	);
	$this->model('model')->add_member($_SESSION['uid'],$post);
	$item['diy_titles']=$item['diy_titles']?json_decode($item['diy_titles'],true):array();
	$item['video_datas']=$item['video_datas']?json_decode($item['video_datas'],true):array();
	$item['poster_data']=$item['poster_data']?json_decode($item['poster_data'],true):array('data'=>$this->poster_datademo,'image'=>'../addons/jy_douyintui/resource/images/poster_bg.jpg');
	$item['coupons']=$item['coupons']?json_decode($item['coupons'],true):array();
	$item['starttime2']=$item['starttime'];
	$item['endtime2']=$item['endtime'];
	$item['starttime'] =$item['starttime']?date('Y/m/d H:i',$item['starttime']):'';
	$item['endtime'] =$item['endtime']?date('Y/m/d H:i',$item['endtime']):'';
	$isget=$this->pdo_get('coupon_log',array('mid'=>$_SESSION['uid'],'coupon_id'=>$item['coupons'][0]['coupon_id'],'game_id'=>$item['id']));
	if($isget['id']){
		$return['isgetcoupon']=1;
	}
	$return['item']=$item;
	if($item['starttime2']>time()){
		$return['gameerror']=array('error'=>1,'message'=>'活动'.$item['starttime'].'开始，目前不能参与活动！');
		exi($return);
	}
	if($item['endtime2']<time()){
		$return['gameerror']=array('error'=>1,'message'=>'活动'.$item['endtime'].'已经结束，目前不能参与活动！');
		exi($return);
	}
	if($return['item']['saonum']){
		$issaonum=$this->pdo_count('video_log',array('uniacid'=>$_SESSION['uniacid'],'game_id'=>$return['item']['id']),'id');
		if($issaonum>=$return['item']['saonum']){
			$return['gameerror']=array('error'=>1,'message'=>'扫码次数已达上限，不能参与活动！');
			exi($return);
		}
	}
	$this->model('model')->add_saolog($item);
	$isdiannum=$this->model('model')->check_diannum($item);
	if(!$isdiannum){
		$return['gameerror']=array('error'=>1,'message'=>'商户点数不够，目前不能参与活动！');
		exi($return);
	}
	$isup=$this->model('model')->check_upvideo($item);
	if($isup['id']){
		if($item['linktype']==1){
			$golink=$item['douyinurl'];
		}elseif($item['linktype']==2){
			$golink=$item['linkurl'];
		}else{
			$golink='jy_weishop/coupon.myindex/';
		}
		$this->model('model')->upvideodata($isup);
		$return['golink']=$golink;
		exi($return);
		//exi('','',$golink);
	}elseif($isdiannum){
		$upvideo=$this->model('model')->upvideo($item['video_datas'][0]['url']);
		if(!$upvideo['error']){
			if($upvideo['video']['video_id']){
				$createvideo=$this->model('model')->createvideo($upvideo['video']['video_id'],$item);
			}
			if(!$createvideo['error']){
				$item_id=$createvideo['item_id'];
				$this->model('model')->add_video_log($item,$item_id);
			}
		}
	}
	$return['upvideo']=$upvideo;
	exi($return);
}
if($_RQ['op']=='getcoupon'){
	$post=get_postdata();
	$coupons=$post['coupons'];
	foreach($coupons as $cv){
		$coupon_log=array(
			'game_id' => $post['id'],
			'coupon_id'=>$cv['coupon_id'],
		);
		$this->model('model')->add_coupon_log($coupon_log);
		$isget=true;
	}
	if($isget){
		exi('领取成功！！','error');
	}else{
		exi('没有优惠券可领！','error');
	}
}